﻿using System;
using System.Collections.Generic;
using System.Linq;
using AccountManagement.DataAccess;

namespace AccountManagement.BussinessObjects
{
    class IncomingBO
    {
        private readonly LinqHelper<AccountsDataContext> _helper = new LinqHelper<AccountsDataContext>();

        /// <summary>
        /// Get all Incoming
        /// </summary>
        /// <returns>List Incoming</returns>
        public List<tblPhieuThuChi> GetAll()
        {
            try
            {
                return _helper.GetTable<tblPhieuThuChi>().ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public tblPhieuThuChi GetById(int id)
        {
            try
            {
                return _helper.GetTable<tblPhieuThuChi>().Where(t => t.IsIncoming && t.ID == id).SingleOrDefault();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool IsExisted(int quyenSo, int phieuSo, bool isIncoming)
        {
            try
            {
                //var test = _helper.GetTable<tblPhieuThuChi>().Where(t=>t.QuyenSo == quyenSo && t.PhieuSo == phieuSo);
                bool ret = _helper.EntityExists<tblPhieuThuChi>(t => t.QuyenSo == quyenSo && t.PhieuSo == phieuSo && t.IsIncoming == isIncoming);
                return ret;
            }
            catch (Exception)
            {
                return true;
            }
        }

        public int Insert(tblPhieuThuChi objPhieuThuChi)
        {
            try
            {
                decimal soDuTkThue = 0;

                // Check if existed
                if (IsExisted(objPhieuThuChi.QuyenSo, objPhieuThuChi.PhieuSo, objPhieuThuChi.IsIncoming))
                    return Constants.Errors.EXISTED;

                // Check if TK_NO existed
                var tkNo = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKNo);
                if (tkNo == null)
                    return Constants.Errors.TK_NO_NOT_EXISTED;

                // Check if TK_CO existed
                var tkCo = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKCo);
                if (tkCo == null)
                    return Constants.Errors.TK_CO_NOT_EXISTED;
                
                // Check if TK_THUE existed
                tblAccount tkThue = null;
                if (objPhieuThuChi.TKThue != null)
                {
                    tkThue = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKThue);
                    if (tkThue == null)
                        return Constants.Errors.TK_THUE_NOT_EXISTED;
                    soDuTkThue = tkThue.SoDu;
                }

                // Store So du TK
                var soDuTkCo = tkCo.SoDu;
                var soDuTkNo = tkNo.SoDu;

                // Update TKs
                if (objPhieuThuChi.IsIncoming)
                {
                    tkNo.SoDu = tkNo.SoDu - (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                    tkCo.SoDu = tkCo.SoDu + objPhieuThuChi.Sotien;
                }
                else
                {
                    tkNo.SoDu = tkNo.SoDu - objPhieuThuChi.Sotien;
                    tkCo.SoDu = tkCo.SoDu + (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                }

                if (tkThue != null)
                {
                    // If is Incomming (thu)
                    // Else is Outgoing (chi)
                    if (objPhieuThuChi.IsIncoming)
                    {
                        tkThue.SoDu += TaxCalculator(objPhieuThuChi);
                    }
                    else
                    {
                        tkThue.SoDu -= TaxCalculator(objPhieuThuChi);
                    }
                }

                // Begin transaction
                _helper.BeginTransaction();

                // Insert into DB
                _helper.Insert(objPhieuThuChi);

                //_helper.Update(tkNo, t => t.SoTK == objPhieuThuChi.TKNo);
                //_helper.Update(tkCo, t => t.SoTK == objPhieuThuChi.TKCo);
                //if (tkThue != null)
                //    _helper.Update(tkThue, t => t.SoTK == objPhieuThuChi.TKThue);
                UpdateAccount(tkNo, tkNo.SoDu - soDuTkNo);
                UpdateAccount(tkCo, tkCo.SoDu - soDuTkCo);
                if (tkThue != null)
                    UpdateAccount(tkThue, tkThue.SoDu - soDuTkThue);

                // Commit transaction
                _helper.Transaction.CommitAndDispose();

                // Update SoPhieu thu/chi
                if (objPhieuThuChi.IsIncoming)
                {
                    Utils.SetPhieuSoThu(objPhieuThuChi.PhieuSo + 1);
                }
                else
                {
                    Utils.SetPhieuSoChi(objPhieuThuChi.PhieuSo + 1);
                }

                return objPhieuThuChi.IsIncoming ? Constants.Errors.NONE_ADD_PHIEUTHU : Constants.Errors.NONE_ADD_PHIEUCHI;
            }
            catch (Exception)
            {
                _helper.Transaction.RollbackAndDispose();
                return Constants.Errors.UNKNOW;
            }
        }

        public int Update(tblPhieuThuChi objPhieuThuChi)
        {
            try
            {
                // Get object before update
                var objBeforeUpdate = _helper.GetOne<tblPhieuThuChi>(t => t.ID == objPhieuThuChi.ID) ??
                                      _helper.GetOne<tblPhieuThuChi>(
                                          t =>
                                          t.QuyenSo == objPhieuThuChi.QuyenSo && t.PhieuSo == objPhieuThuChi.PhieuSo &&
                                          t.IsIncoming == objPhieuThuChi.IsIncoming);

                if (objBeforeUpdate == null) return Constants.Errors.NOT_EXISTED;

                // Begin transaction
                _helper.BeginTransaction();

                // Check if amount changed
                if (objBeforeUpdate.Sotien != objPhieuThuChi.Sotien)
                {
                    // Restore TK_NO balance
                    var tkNo = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKNo);
                    var soDuTkNo = tkNo.SoDu;
                    if (objPhieuThuChi.IsIncoming)
                    {
                        tkNo.SoDu = tkNo.SoDu + (objBeforeUpdate.Sotien + TaxCalculator(objBeforeUpdate));
                    }
                    else
                    {
                        tkNo.SoDu = tkNo.SoDu + objBeforeUpdate.Sotien;
                    }

                    // Restore TK_CO balance
                    var tkCo = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKCo);
                    var soDuTkCo = tkCo.SoDu;
                    if (objPhieuThuChi.IsIncoming)
                    {
                        tkCo.SoDu = tkCo.SoDu - objBeforeUpdate.Sotien;
                    }
                    else
                    {
                        tkCo.SoDu = tkCo.SoDu - (objBeforeUpdate.Sotien + TaxCalculator(objBeforeUpdate));
                    }

                    // Restore TK_THUE balance
                    var tkThue = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKThue);
                    decimal soDuTkThue = 0;
                    if (tkThue != null)
                    {
                        soDuTkThue = tkThue.SoDu;
                        if (objPhieuThuChi.IsIncoming)
                        {
                            tkThue.SoDu -= TaxCalculator(objBeforeUpdate);
                        }
                        else
                        {
                            tkThue.SoDu += TaxCalculator(objBeforeUpdate);
                        }
                    }

                    // Check if TK_NO changed
                    if (objBeforeUpdate.TKNo != objPhieuThuChi.TKNo)
                    {
                        // If changed then update new TK_NO balance
                        var tkNoNew = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKNo);
                        if (tkNoNew == null)
                        {
                            _helper.Transaction.RollbackAndDispose();
                            return Constants.Errors.TK_NO_NOT_EXISTED;
                        }

                        var soDu = tkNoNew.SoDu;

                        if (objPhieuThuChi.IsIncoming)
                        {
                            tkNoNew.SoDu = tkNoNew.SoDu - (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                        }
                        else
                        {
                            tkNoNew.SoDu = tkNoNew.SoDu - objPhieuThuChi.Sotien;
                        }
                        //_helper.Update(tkNoNew, t => t.SoTK == tkNoNew.SoTK);
                        UpdateAccount(tkNoNew,tkNoNew.SoDu - soDu);
                    }
                    else
                    {
                        // Else update TK_NO balance
                        if (objPhieuThuChi.IsIncoming)
                        {
                            tkNo.SoDu = tkNo.SoDu - (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                        }
                        else
                        {
                            tkNo.SoDu = tkNo.SoDu - objPhieuThuChi.Sotien;
                        }
                    }

                    // Check if TK_CO changed
                    if (objBeforeUpdate.TKCo != objPhieuThuChi.TKCo)
                    {
                        // If changed then update new TK_CO balance
                        var tkCoNew = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKCo);
                        if (tkCoNew == null)
                        {
                            _helper.Transaction.RollbackAndDispose();
                            return Constants.Errors.TK_CO_NOT_EXISTED;
                        }

                        var soDu = tkCoNew.SoDu;

                        if (objPhieuThuChi.IsIncoming)
                        {
                            tkCoNew.SoDu = tkCoNew.SoDu + objPhieuThuChi.Sotien;
                        }
                        else
                        {
                            tkCoNew.SoDu = tkCoNew.SoDu + (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));   
                        }
                        
                        //_helper.Update(tkCoNew, t => t.SoTK == tkCoNew.SoTK);
                        UpdateAccount(tkCoNew,tkCoNew.SoDu - soDu);
                    }
                    else
                    {
                        // Else update TK_CO balance
                        if (objPhieuThuChi.IsIncoming)
                        {
                            tkCo.SoDu = tkCo.SoDu + objPhieuThuChi.Sotien;
                        }
                        else
                        {
                            tkCo.SoDu = tkCo.SoDu + (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                        }
                    }

                    // Check if TK_THUE changed
                    if (objBeforeUpdate.TKThue != objPhieuThuChi.TKThue)
                    {
                        // If changed then update new TK_THUE balance
                        var tkThueNew = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKThue);
                        if (tkThueNew == null)
                        {
                            _helper.Transaction.RollbackAndDispose();
                            return Constants.Errors.TK_THUE_NOT_EXISTED;
                        }

                        var soDu = tkThueNew.SoDu;

                        if (objPhieuThuChi.IsIncoming)
                        {
                            tkThueNew.SoDu += TaxCalculator(objPhieuThuChi);
                        }
                        else
                        {
                            tkThueNew.SoDu -= TaxCalculator(objPhieuThuChi);
                        }
                        //_helper.Update(tkThueNew, t => t.SoTK == tkThueNew.SoTK);
                        UpdateAccount(tkThueNew, tkThueNew.SoDu - soDu);
                    }
                    else
                    {
                        if (tkThue != null)
                        {
                            // Else update TK_THUE balance
                            if (objPhieuThuChi.IsIncoming)
                            {
                                tkThue.SoDu += TaxCalculator(objPhieuThuChi);
                            }
                            else
                            {
                                tkThue.SoDu -= TaxCalculator(objPhieuThuChi);
                            }
                        }
                    }

                    // Commit old Accounts - we don't care if they changed or not
                    //_helper.Update(tkNo, t => t.SoTK == tkNo.SoTK);
                    //_helper.Update(tkCo, t => t.SoTK == tkCo.SoTK);
                    //if (tkThue != null)
                    //    _helper.Update(tkThue, t => t.SoTK == tkThue.SoTK);
                    UpdateAccount(tkNo,tkNo.SoDu - soDuTkNo);
                    UpdateAccount(tkCo, tkCo.SoDu - soDuTkCo);
                    if (tkThue != null)
                        UpdateAccount(tkThue, tkThue.SoDu - soDuTkThue);
                }
                else
                {
                    // Check if TK_NO changed
                    if (objBeforeUpdate.TKNo != objPhieuThuChi.TKNo)
                    {
                        // If changed then update TK_NO and TK_NO new balance
                        // Get Accounts
                        var tkNo = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKNo);
                        var tkNoNew = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKNo);
                        if (tkNoNew == null)
                        {
                            _helper.Transaction.RollbackAndDispose();
                            return Constants.Errors.TK_NO_NOT_EXISTED;
                        }

                        var soDu = tkNo.SoDu;
                        var soDuNew = tkNoNew.SoDu;

                        // Update balance
                        if (objPhieuThuChi.IsIncoming)
                        {
                            tkNo.SoDu = tkNo.SoDu + (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                            tkNoNew.SoDu = tkNoNew.SoDu - (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                        }
                        else
                        {
                            tkNo.SoDu = tkNo.SoDu + objPhieuThuChi.Sotien;
                            tkNoNew.SoDu = tkNoNew.SoDu - objPhieuThuChi.Sotien;
                        }
                        // Commit them to DB
                        //_helper.Update(tkNo, t => t.SoTK == tkNo.SoTK);
                        //_helper.Update(tkNoNew, t => t.SoTK == tkNoNew.SoTK);
                        UpdateAccount(tkNo, tkNo.SoDu - soDu);
                        UpdateAccount(tkNoNew, tkNoNew.SoDu - soDuNew);
                    }

                    // Check if TK_CO changed
                    if (objBeforeUpdate.TKCo != objPhieuThuChi.TKCo)
                    {
                        // If changed then update TK_CO and TK_CO new balance
                        // Get Accounts
                        var tkCo = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKCo);
                        var tkCoNew = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKCo);
                        if (tkCoNew == null)
                        {
                            _helper.Transaction.RollbackAndDispose();
                            return Constants.Errors.TK_CO_NOT_EXISTED;
                        }

                        var soDu = tkCo.SoDu;
                        var soDuNew = tkCoNew.SoDu;

                        // Update balance
                        if (objPhieuThuChi.IsIncoming)
                        {
                            tkCo.SoDu = tkCo.SoDu - objPhieuThuChi.Sotien;
                            tkCoNew.SoDu = tkCoNew.SoDu + objPhieuThuChi.Sotien;
                        }
                        else
                        {
                            tkCo.SoDu = tkCo.SoDu - (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                            tkCoNew.SoDu = tkCoNew.SoDu + (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                        }
                        // Commit them to DB
                        //_helper.Update(tkCo, t => t.SoTK == tkCo.SoTK);
                        //_helper.Update(tkCoNew, t => t.SoTK == tkCoNew.SoTK);
                        UpdateAccount(tkCo, tkCo.SoDu - soDu);
                        UpdateAccount(tkCoNew, tkCoNew.SoDu - soDuNew);
                    }

                    // Check if TK_THUE changed
                    if (objBeforeUpdate.TKThue != objPhieuThuChi.TKThue)
                    {
                        // If changed then update TK_CO and TK_CO new balance
                        // Get Accounts
                        var tkThue = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKThue);
                        var tkThueNew = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKThue);
                        if (tkThueNew == null)
                        {
                            _helper.Transaction.RollbackAndDispose();
                            return Constants.Errors.TK_THUE_NOT_EXISTED;
                        }

                        var soDu = tkThue.SoDu;
                        var soDuNew = tkThueNew.SoDu;

                        // If is Incomming
                        if (objPhieuThuChi.IsIncoming)
                        {
                            // Update balance
                            tkThue.SoDu = tkThue.SoDu - TaxCalculator(objPhieuThuChi);
                            tkThueNew.SoDu = tkThueNew.SoDu + TaxCalculator(objPhieuThuChi);
                        }
                        else
                        {
                            // Update balance
                            tkThue.SoDu = tkThue.SoDu + TaxCalculator(objPhieuThuChi);
                            tkThueNew.SoDu = tkThueNew.SoDu - TaxCalculator(objPhieuThuChi);
                        }
                        // Commit them to DB
                        //_helper.Update(tkThue, t => t.SoTK == tkThue.SoTK);
                        //_helper.Update(tkThueNew, t => t.SoTK == tkThueNew.SoTK);
                        UpdateAccount(tkThue, tkThue.SoDu - soDu);
                        UpdateAccount(tkThueNew, tkThueNew.SoDu - soDuNew);
                    }

                    // Check if Thue has changed
                    if (objBeforeUpdate.Thue != objPhieuThuChi.Thue && objBeforeUpdate.TKThue != null)
                    {
                        // If changed then update TK_Thue new balance
                        // Get Accounts
                        var tkNo = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKNo);
                        var tkCo = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKCo);
                        var tkThue = _helper.GetOne<tblAccount>(t => t.SoTK == objBeforeUpdate.TKThue);

                        var soDuNo = tkNo.SoDu;
                        var soDuCo = tkCo.SoDu;
                        var soDuThue = tkThue.SoDu;

                        // If is Incomming
                        if (objPhieuThuChi.IsIncoming)
                        {
                            var oldTax = TaxCalculator(objBeforeUpdate);
                            var newTax = TaxCalculator(objPhieuThuChi);
                            // Update balance
                            tkThue.SoDu = tkThue.SoDu - oldTax;
                            tkThue.SoDu = tkThue.SoDu + newTax;

                            tkNo.SoDu = tkNo.SoDu + oldTax;
                            tkNo.SoDu = tkNo.SoDu - newTax;

                            //tkCo.SoDu = tkCo.SoDu - oldTax;
                            //tkCo.SoDu = tkCo.SoDu + newTax;
                        }
                        else
                        {
                            var oldTax = TaxCalculator(objBeforeUpdate);
                            var newTax = TaxCalculator(objPhieuThuChi);
                            // Update balance
                            tkThue.SoDu = tkThue.SoDu + oldTax;
                            tkThue.SoDu = tkThue.SoDu - newTax;

                            //tkNo.SoDu = tkNo.SoDu + oldTax;
                            //tkNo.SoDu = tkNo.SoDu - newTax;

                            tkCo.SoDu = tkCo.SoDu - oldTax;
                            tkCo.SoDu = tkCo.SoDu + newTax;
                        }
                        // Commit them to DB
                        //_helper.Update(tkThue, t => t.SoTK == tkThue.SoTK);
                        //_helper.Update(tkNo, t => t.SoTK == tkNo.SoTK);
                        //_helper.Update(tkCo, t => t.SoTK == tkCo.SoTK);
                        UpdateAccount(tkThue, tkThue.SoDu - soDuThue);
                        UpdateAccount(tkNo, tkNo.SoDu - soDuNo);
                        UpdateAccount(tkCo, tkCo.SoDu - soDuCo);
                    }
                }

                // Update 
                if (objPhieuThuChi.ID != 0)
                    _helper.Update(objPhieuThuChi, t => t.ID == objPhieuThuChi.ID);
                else
                {
                    // Update when Import
                    objPhieuThuChi.ID = objBeforeUpdate.ID;
                    _helper.Update(objPhieuThuChi,
                                   t =>
                                   t.PhieuSo == objPhieuThuChi.PhieuSo && t.QuyenSo == objPhieuThuChi.QuyenSo &&
                                   t.IsIncoming == objPhieuThuChi.IsIncoming);
                }

                // Commit transaction
                _helper.Transaction.CommitAndDispose();

                return objPhieuThuChi.IsIncoming ? Constants.Errors.NONE_EDIT_PHIEUTHU : Constants.Errors.NONE_EDIT_PHIEUCHI;
            }
            catch (Exception)
            {
                _helper.Transaction.RollbackAndDispose();
                return Constants.Errors.UNKNOW;
            }
        }

        public int Delete(int id)
        {
            try
            {
                var objPhieuThuChi = _helper.GetOne<tblPhieuThuChi>(t => t.ID == id);

                if (objPhieuThuChi == null)
                {
                    return Constants.Errors.NOT_EXISTED;
                }

                // Begin transaction
                _helper.BeginTransaction();

                // Update TK_NO
                //var tkNo = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKNo);
                var tkNo = _helper.GetTable<tblAccount>().Where(t => t.SoTK == objPhieuThuChi.TKNo).SingleOrDefault();
                if (tkNo != null)
                {
                    var soDu = tkNo.SoDu;
                    if (objPhieuThuChi.IsIncoming)
                        tkNo.SoDu = tkNo.SoDu + (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                    else
                        tkNo.SoDu = tkNo.SoDu + objPhieuThuChi.Sotien;
                    //_helper.Update(tkNo,t=>t.SoTK == tkNo.SoTK);
                    UpdateAccount(tkNo, tkNo.SoDu - soDu);
                }

                // Update TK_CO
                var tkCo = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKCo);
                if (tkCo != null)
                {
                    var soDu = tkCo.SoDu;
                    if (objPhieuThuChi.IsIncoming)
                        tkCo.SoDu = tkCo.SoDu - objPhieuThuChi.Sotien;
                    else
                        tkCo.SoDu = tkCo.SoDu - (objPhieuThuChi.Sotien + TaxCalculator(objPhieuThuChi));
                    //_helper.Update(tkCo, t => t.SoTK == tkCo.SoTK);
                    UpdateAccount(tkCo, tkCo.SoDu - soDu);
                }

                // Update TK_THUE
                var tkThue = _helper.GetOne<tblAccount>(t => t.SoTK == objPhieuThuChi.TKThue);
                if (tkThue != null)
                {
                    var soDu = tkThue.SoDu;
                    // If is Incomming (thu)
                    // Else is Outgoing (chi)
                    if (objPhieuThuChi.IsIncoming)
                    {
                        tkThue.SoDu -= TaxCalculator(objPhieuThuChi);
                    }
                    else
                    {
                        tkThue.SoDu += TaxCalculator(objPhieuThuChi);
                    }
                    //_helper.Update(tkThue, t => t.SoTK == tkThue.SoTK);
                    UpdateAccount(tkThue, tkThue.SoDu - soDu);
                }

                // Delete
                _helper.Delete<tblPhieuThuChi>(t => t.ID == objPhieuThuChi.ID);

                // Commit transaction
                _helper.Transaction.CommitAndDispose();

                return objPhieuThuChi.IsIncoming ? Constants.Errors.NONE_DELETE_PHIEUTHU : Constants.Errors.NONE_DELETE_PHIEUCHI;
            }
            catch (Exception)
            {
                _helper.Transaction.RollbackAndDispose();
                return Constants.Errors.UNKNOW;
            }
        }

        private static decimal TaxCalculator(tblPhieuThuChi objPhieuThuChi)
        {
            try
            {
                if (objPhieuThuChi.TKThue == null || objPhieuThuChi.Thue == 0)
                    return 0;
                return Math.Round(Convert.ToDecimal((objPhieuThuChi.Sotien*objPhieuThuChi.Thue)/100), 2);
            }
            catch (Exception)
            {
                return 0;
            }
        }

        private void UpdateAccount(tblAccount objAccount, decimal amount)
        {
            try
            {
                _helper.Update(objAccount, t => t.SoTK == objAccount.SoTK);

                if (objAccount.TKCha != null)
                {
                    var objTkCha = _helper.GetOne<tblAccount>(t => t.SoTK == objAccount.TKCha);
                    objTkCha.SoDu = objTkCha.SoDu + amount;

                    UpdateAccount(objTkCha, amount);
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        #region Search
        public List<tblPhieuThuChi> SearchByName(string name)
        {
            try
            {
                return _helper.GetTable<tblPhieuThuChi>().Where(t => t.HoTen.Contains(name)).OrderBy(t => t.NgayThang).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public List<tblPhieuThuChi> SearchByDate(DateTime date)
        {
            try
            {
                //return _helper.GetTable<tblPhieuThuChi>().Where(t => t.NgayThang.ToShortDateString() == date.ToShortDateString()).ToList();
                //var lstObj = _helper.GetTable<tblPhieuThuChi>().ToList();
                return _helper.GetTable<tblPhieuThuChi>().ToList().Where(phieuThuChi => phieuThuChi.NgayThang.ToShortDateString() == date.ToShortDateString()).ToList();
            }
            catch (Exception)
            {
                return null;
            }
        }
        #endregion
    }
}
